package org.an.proj.contacts.adapter;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;
import de.hdodenhof.circleimageview.CircleImageView;
import org.an.ku.base.adapter.BaseSimpleAdapter;
import org.an.ku.base.itf.OnItemClickListener;
import org.an.ku.view.MyTextView;
import org.an.ku.view.indexview.PinYinUtil;
import org.an.proj.contacts.R;
import org.an.proj.contacts.model.ContactInfo;

/**
 * 创建时间：2016年08月30日20:14 <br>
 * 作者：fuchaoyang <br>
 * 描述：系统联系人列表adapter
 */
public class ContactAdapter extends BaseSimpleAdapter<ContactInfo, ContactAdapter.ViewHolder> {

  private OnItemClickListener<ContactInfo> mOnItemClickListener;

  public ContactAdapter(Context context, OnItemClickListener<ContactInfo> onItemClickListener) {
    super(context);
    this.mOnItemClickListener = onItemClickListener;
  }

  @Override public int getLayoutId() {
    return R.layout.item_contact;
  }

  @Override public ViewHolder onCreateViewHolder(View convertView) {
    return new ViewHolder(convertView);
  }

  @Override public void onBindViewHolder(final ViewHolder holder, final ContactInfo contactInfo,
      final int position) {
    holder.mIvAvatar.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        if (mOnItemClickListener != null) {
          mOnItemClickListener.onItemClick(position, contactInfo, v);
        }
      }
    });

    holder.mBtnDelete.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        // 必须关闭
        holder.mSwipeLayout.quickClose();
        if (mOnItemClickListener != null) {
          mOnItemClickListener.onItemClick(position, contactInfo, v);
        }
      }
    });

    holder.mTvName.setText(contactInfo.getName());
    char index = getIndex(position);
    //if (position == 0 || PinYinUtil.getIndex(getItem(position - 1).name) != index) {
    if (position == 0 || getIndex(position - 1) != index) {
      holder.mTvIndex.setVisibility(View.VISIBLE);
      holder.mTvIndex.setText(String.valueOf(index));
    } else {
      holder.mTvIndex.setVisibility(View.GONE);
    }

    // 隐藏
    //if (position == getCount() - 1 || PinYinUtil.getIndex(getItem(position + 1).name) != index) {
    if (position == getCount() - 1 || getIndex(position + 1) != index) {
      holder.mDivider.setVisibility(View.GONE);
    } else {
      holder.mDivider.setVisibility(View.VISIBLE);
    }
  }

  /**
   * 获取指定position出的index 如:安小生 —— A
   *
   * @param position position
   * @return A
   */
  private char getIndex(int position) {
    return PinYinUtil.getIndex(getItem(position).getPinyin());
  }

  static class ViewHolder {
    @Bind(R.id.tv_index) MyTextView mTvIndex;
    @Bind(R.id.iv_avatar) CircleImageView mIvAvatar;
    @Bind(R.id.tv_name) TextView mTvName;
    @Bind(R.id.divider) View mDivider;
    @Bind(R.id.btn_edit) Button mBtnEdit;
    @Bind(R.id.btn_delete) Button mBtnDelete;
    @Bind(R.id.swipe_layout) SwipeMenuLayout mSwipeLayout;

    ViewHolder(View view) {
      ButterKnife.bind(this, view);
    }
  }
}
